<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .selected {
      color: red;
    }
  </style>
  <body>
    <div id="root"></div>

    <script src="../lib/react.js"></script>
    <script src="../lib/react-dom.js"></script>
    <script src="../lib/babel.js"></script>

    <script type="text/babel">
      // let message = "Hello World"
      // function btnClick(){
      //     console.log("btnClick");
      //     message = "Hello react"
      //     rootRender()
      // }
      // const root = ReactDOM.createRoot(document.querySelector("#root"))
      // function rootRender(){
      //     root.render(
      //     <div>
      //         <h2>{message}</h2>
      //         <button onClick={btnClick}> 修改</button>
      //     </div>
      //     )
      // }
      // rootRender()
      // 使用组件进行重构代码
      // 类组件和函数式组件

      class App extends React.Component {
        // 组件数据
        constructor() {
          super();
          this.state = {
            movies: ["星际穿越", "斗破苍穹", "武动乾坤", "吞噬星空"],
            currentIndex: 0,
          };

          // 对需要绑定的方法，提前绑定好this
        }
        // 组件方法 (实例方法)
        btnClick(index) {
          console.log(index);
          this.setState({
            currentIndex : index
          })
        }
        // 渲染内容  render方法
        render() {
          const { movies, currentIndex } = this.state;
          return (
            <div>
              <ul>
                {movies.map((movie, index) => {
                  return (
                    <li
                      className={currentIndex == index ? "selected" : ""}
                      onClick={()=>this.btnClick(index)}
                      key={index}
                    >
                      {movie}
                    </li>
                  );
                })}
              </ul>
            </div>
          );
        }
      }

      // 将组件渲染到界面上
      const root = ReactDOM.createRoot(document.querySelector("#root"));
      root.render(<App />);
    </script>
  </body>
</html>
